package com.bsoft.gol.hbs.dao;

import com.bsoft.gol.hbs.service.impl.sysroute.dto.ServiceConfigListDTO;
import com.bsoft.gol.hbs.service.impl.sysroute.vo.ServiceConfigListVO;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import hcn.base.ServItemConf;
import org.atag.dao.action.QoHibernateStatelessResultAction;
import org.atag.dao.ext.HibernateExtendDAO;
import org.atag.dao.model.SimplePageInfo;
import org.springframework.util.StringUtils;

/**
 * description
 *
 * @author neoLin - 2022/09/21 14:06
 **/
public abstract class ServItemConfDAO extends HibernateExtendDAO<ServItemConf> {

    /**
     * 查询服务配置列表
     */
    public SimplePageInfo<ServiceConfigListVO> getServiceConfigList(ServiceConfigListDTO dto) {
        QoHibernateStatelessResultAction<ServiceConfigListVO, ServiceConfigListDTO> action =
                new QoHibernateStatelessResultAction<>(true, dto, ServiceConfigListVO.class);
        String baseSql = "SELECT {{id AS id,tenant_id AS tenantId,org_cd AS orgCd,service_intro AS serviceIntro," +
                "service_name AS serviceName,service_type AS serviceType,service_version AS serviceVersion," +
                "sys_type AS sysType,restful_uri AS restfulUri,webservice_name AS webserviceName," +
                "op_name AS opName,update_time AS updateTime,rpc_service_id AS rpcServiceId," +
                "rpc_service_method AS rpcServiceMethod, invoke_direction AS invokeDirection}}" +
                " FROM serv_item_conf WHERE 1=1 ";
        action.setSql(baseSql);
        action.addCondition(" AND tenant_id=:tenantId ");
        action.addCondition(" AND org_cd=:orgCd ");
        if(StringUtils.isEmpty(dto.getOrgCd())){
            action.addSql(" AND org_cd = '' ");
        }
        action.addCondition(" AND sys_type=:sysType ");
        action.addCondition(" AND invoke_direction=:invokeDirection ");
        action.addCondition(" AND instr like :%instr% ");
        HibernateSessionTemplate.instance().execute(action);
        return action.getSimpleResultPage();
    }

    @DAOMethod(sql = " DELETE FROM ServItemConf WHERE tenantId=:tenantId AND orgCd=:orgCd ")
    public abstract void deleteServiceConfig(@DAOParam("tenantId") String tenantId, @DAOParam("orgCd") String orgCd);

    @DAOMethod(sql = "FROM ServItemConf WHERE tenantId=:tenantId AND orgCd=:orgCd AND serviceName=:serviceName ")
    public abstract ServItemConf getByManageUnit(@DAOParam("tenantId") String tenantId, @DAOParam("orgCd") String orgCd
            , @DAOParam("serviceName") String serviceName);
}
